/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.dao;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author hxg
 */
public class DaoConfig {

    final static File conf = new File(".\\conf\\DaoConf.cfg");

    public static <T extends DataSource> DataSource getDao(DaoSource daoName) {
        return daoName.getDataSource();
    }

    public static void save() {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("DaoSourceConfig");
        //for (Map.Entry<DataSourceEnum, List<DataSource>> entry : mp.entrySet()) {
        //DataSourceEnum name = entry.getKey();

        //int local = 0;
        for (DaoSource enm : DaoSource.values()) {
            if (enm.isActive()) {
                Element sourceEle = root.addElement("DataSouce");
                sourceEle.addElement("name").addText(enm.name());
                Element classesEle = sourceEle.addElement("classes");
                int local = enm.getDataSourceClassList().indexOf(enm.getDefaultDataSourceClass());
                classesEle.addAttribute("default", String.valueOf(local));
                for (Class<? extends DataSource> c : enm.getDataSourceClassList()) {
                    classesEle.addElement("class").addText(c.getName());
                }
            }
        }
        try {
            OutputFormat opf = OutputFormat.createPrettyPrint();
            opf.setLineSeparator("\r\n");//这是为了换行操作  
            XMLWriter outPut = new XMLWriter(new FileWriter(conf, false), opf);
            outPut.write(doc);
            outPut.close();
        } catch (IOException ex) {
            Logger.getLogger(DaoConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void load() {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(conf);
            for (Element daoEle : (List<Element>) doc.getRootElement().elements("DataSouce")) {
                try {
                    String name = daoEle.element("name").getText();
                    DaoSource daoName = DaoSource.valueOf(name);
                    Element classesEle = daoEle.element("classes");
                    String defaultLocal = classesEle.attributeValue("default");
                    int local = 0;
                    if (defaultLocal != null) {
                        local = Integer.parseInt(defaultLocal);
                    }
                    List<Element> classElelist = (List<Element>) classesEle.elements("class");
                    for (int i = 0; i < classElelist.size(); i++) {
                        Element ele = classElelist.get(i);
                        String className = ele.getText();
                        Class<? extends DataSource> c = (Class<? extends DataSource>) (Class.forName(className));
                        if (i == local) {
                            daoName.addDataSource(c, true);
                        } else {
                            daoName.addDataSource(c, false);
                        }
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(DaoConfig.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (DocumentException ex) {
            Logger.getLogger(DaoConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
